/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp;

import com.mydatastore.DataStoreException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;

/**
 *
 * @author nathaniel
 */
@WebListener
public class MyApp implements ServletContextListener {

  private static String lookup;

  public static Connection getConnection() throws DataStoreException {
    try {
      Context initContext = new InitialContext();
      DataSource ds = (DataSource) initContext.lookup(lookup);
      return ds.getConnection();
    } catch (NamingException | SQLException ex) {
      throw new DataStoreException(ex.getMessage());
    }
  }

  @Override
  public void contextInitialized(ServletContextEvent sce) {
    lookup = sce.getServletContext().getInitParameter("LOOKUP");
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
  }
}
